/**
 *
 */
package com.et114.modules.org.dao;

import java.util.List;

import com.et114.components.acegi.Constants;
import com.et114.core.common.AppContext;
import com.et114.core.dao.impl.BaseDAOImpl;
import com.et114.core.exception.CheckException;
import com.et114.modules.org.utility.SessionUserInfo;
import com.et114.modules.org.vo.UserEmplyVo;
import com.et114.modules.org.vo.UserRoleRelVo;
import com.et114.modules.org.vo.UserVo;

/**
 * @author guanhw
 */
public class UserDAO extends BaseDAOImpl {
	
	public List getUsers ( UserVo vo ) throws CheckException {
		return queryList ( vo , "getUsers" );
	}
	
	public List getSearchUsers ( UserVo vo ) throws CheckException {
		return queryList ( vo , "getSearchUsers" );
	}
	
	public UserVo getUserById ( Long userId ) throws CheckException {
		return ( UserVo ) sqlMapClientTemplate.queryForObject ( "getUserById" , userId );
	}
	
	public List getUsers ( ) throws CheckException {
		return getUsers ( new UserVo ( ) );
	}
	
	public List getUsersNcRegUser( UserVo vo ) throws CheckException {
		return queryList( vo  , "getUsersNcRegUser"  ) ;
	}
	
	public List getRolesByUser ( UserVo vo ) throws CheckException {
		return queryList ( vo , "getRolesByUser" );
	}
	
	
	public Long countgetRolesByUser( UserVo vo ) throws CheckException { 
		return getCount( vo , "countgetRolesByUser" ) ; 
	}
	
	public List getRolesByUserNPage ( UserVo vo ) throws CheckException {
		return queryList ( vo , "getRolesByUserNPage" );
	}	
	
	public List getRolesByUser ( java.util.Map params ) throws CheckException {
		return queryList ( params , "getRolesByUserParams" );
	}
	
	public List getAllRolesByUserParams( java.util.Map params ) throws CheckException { 
		return queryList ( params , "getAllRolesByUserParams" );
	}
	
	public SessionUserInfo getSessionUserInfoByLoginNameAndPassword (
			String loginName , String password ) throws CheckException {
		java.util.Map map = new java.util.HashMap ( 2 );
		map.put ( "loginName" , loginName );
		map.put ( "password" , password );
		return ( SessionUserInfo ) load ( map , "getSessionUserInfoByLoginNameAndPassword" );
	}
	
	public UserEmplyVo getUserEmplyByEmpId ( Long empId ) throws CheckException {
		return ( UserEmplyVo ) sqlMapClientTemplate.queryForObject (
				"getUserEmplyByEmpId" , empId );
	}
	
	
	public Integer getMaxRoleTypeByUserId( Long userId ) throws CheckException {
		return ( Integer )  sqlMapClientTemplate.queryForObject ( "getMaxRoleTypeByUserId" , userId  ) ;
	}
	
	public List getFunctionsByUser ( Long userId ) throws CheckException {
		return queryList ( userId , "getFunctionsByUser" );
	}
	
	public int deleteUserRoleRel ( UserRoleRelVo userRoleRel )
			throws CheckException {
		if ( AppContext.isHasLog (  ) ) {		
			userRoleRel.setOperType ( Constants.LOG_OPER_REMOVE) ;
			addLogUserRoleRel( userRoleRel ) ; 
		}
		return delete ( userRoleRel , "removeUserRoleRel" );
	}
	
	public int deleteUserRoleRelByRoleId ( Long roleId  , Long operUser ) throws CheckException {
		UserRoleRelVo userRoleRel = new UserRoleRelVo() ; 
		userRoleRel.setRoleId ( roleId ) ; 
		userRoleRel.setOperUser ( operUser ) ;
		//return delete ( userRoleRel , "removeUserRoleRel" );
		return deleteUserRoleRel( userRoleRel ) ;
	}
	
	public Long createUser ( UserVo userVo ) throws CheckException {
		return ( Long ) insert ( userVo , "createUser" );
	}
	
	public void updateUser ( UserVo userVo ) throws CheckException {
		update ( userVo , "updateUser" );
	}
	
	public void updateAllUser ( UserVo userVo ) throws CheckException {
		update ( userVo , "updateAllUser" );
	}
	
	public List getEmpAllList ( UserVo userVo ) throws CheckException {
		return queryList ( userVo , "queryEmpAllList" );
	}
	
	public Long countEmpAllList ( UserVo userVo ) throws CheckException {
		return getCount ( userVo , "countEmpAllList" );
	}
	
	public int removeUser ( UserVo userVo ) throws CheckException {
		return delete ( userVo , "removeUser" );
	}
	
	public void createUserRoleRel ( UserRoleRelVo userRoleRel )
			throws CheckException {
		insert ( userRoleRel , "createUserRoleRel" );
		if ( AppContext.isHasLog (  ) ) {		
			userRoleRel.setOperType ( Constants.LOG_OPER_NEW) ;
			addLogUserRoleRel( userRoleRel ) ; 
		}
	}
	
	public Long countLoginName ( String loginName ) throws CheckException {
		return getCount ( loginName , "countUserByLoginName" );
	}
	
	public UserVo loadUserByLoginName ( String loginName )
			throws CheckException {
		return ( UserVo ) load ( loginName , "loadUserByLoginName" );
	}
	
	public UserVo loadUserByEmp ( Long empId ) throws CheckException {
		return ( UserVo ) load ( empId , "loadUserByEmp" );
	}
	
	public boolean checkExistsUserByLoginName( String loginName )  throws CheckException {
		Long check = getCount( loginName , "checkExistsUserByLoginName" ) ; 
		if ( check.longValue ( ) != 0 ) return false ;
		else return true ; 
	}
	
	public void addLogUserRoleRel( UserRoleRelVo userRoleRel )  throws CheckException { 
		insert ( userRoleRel , "addLogUserRoleRel" ) ;
	}
	
	
}